import _util from "./Util";
import _fetch from "./Fetch";
import _router from './Router'

let originPage = Page;
let originComponent = Component;

Page = (opt) => {

  opt = {
    ...opt,
    _util,
    _fetch,
    _router
  }
  return originPage(opt);
}

Component = (opt) => {
  let { props = {} } = opt;
  let properties = {};

  // 优化 properties 传入方式
  Object.keys(props).forEach(item => {
    properties[item] = {
      type: _util.getValueType(props[item]),
      value: props[item],
      observer: function (newVal, oldVal, changedPath) {
        const changeEvent = {
          event: item,
          newVal, oldVal, changedPath
        };
        // 传入属性可通过组件内定义的$watch方法统一监听变化
        this.$watch && this.$watch(changeEvent);
      }
    }
  });
  opt.properties = properties;

  // Component函数的options无法传入参数 只能在某个生命周期函数被调用时动态赋值
  let originAttached = opt.attached || function () {};
  opt.attached = function() {
    this._util = _util;
    this._fetch = _fetch;
    this._router = _router;
    return originAttached.apply(this);
  }
  return originComponent(opt)
}